package com.kehutong.auth.controller;

import javax.servlet.http.HttpServletRequest;

import com.kehutong.common.enums.UserType;
import com.kehutong.common.util.Token;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.ReqMapping;

import com.kehutong.auth.service.LoginEmployeeService;
import org.coraframework.util.Objects;

/**
 * 授权登录
 *
 * @author: liukunlong
 * @date: 2021-03-25 19:19
 */
@MvcAction
@ReqMapping("/admin/login")
public class LoginController {

    private static Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Inject
    private LoginEmployeeService loginService;
    
    // 账号密码登陆
    @ReqMapping("/login")
    public Object login(HttpServletRequest req, String username, String password) throws Exception {
        return loginService.logingByPass(req, username, password);
    }

    @ReqMapping("/corpCheck")
    public Object corpCheck(Token token, String corpNo){
        logger.info("-------------------------------------------------登录检测登录信息 token:{}", Objects.nonNull(token) ? token.toJSON() : "");
        logger.info("-------------------------------------------------登录检测登录信息 corpNo:{}", corpNo);
        if(Objects.isNull(token)){
            return true;
        }
        if(Objects.isEmpty(corpNo)){
            return true;
        }

        if(Objects.equal(corpNo, token.getCorpid())){
            return true;
        }
        return false;
    }

    // 企业微信扫码登陆
    @ReqMapping("/tencent/login")
    public Object wwloginByTencentWeworkQrcode(HttpServletRequest req, String corpId, String code) throws Exception {
        logger.trace("corpid:{}, code:{}", corpId, code);
        return loginService.wwloginByTencentWeworkQrcode(req, corpId, code);
    }

    // 企业微信已oauthor2.0认证方式登录管理后台
    @ReqMapping("/tencent/oauth/login")
    public Object wwloginByTencentWeworkOauth(HttpServletRequest req, String corpId, String code) throws Exception {
        logger.trace("corpid:{}, code:{}, token:{}", corpId, code);
        return loginService.loginByTencentWework(req, corpId, code, UserType.system);
    }

    // 获取登录日志信息
    @ReqMapping("/log")
    public Object logPage(Token token, JSONObject jsonObject){
        return loginService.logPage(token, jsonObject);
    }
    
}
